#include <algorithm>
#include <cstdint>
#include <iostream>
#include <istream>
#include <stdexcept>
#include <vector>

using std::cin, std::cout;
using ll = int64_t;
const ll max_n = 6e3+5;
ll n, r[max_n], dp[max_n][2], ind[max_n];
std::vector<ll> sons[max_n];

void dfs(const ll &now){
    dp[now][1]=r[now];
    for(const ll &next: sons[now]){
        dfs(next);
        dp[now][1] += dp[next][0];
        dp[now][0] += std::max(dp[next][0], dp[next][1]);
    }
}

int main(){
    std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    cin>>n;
    for(ll i{1};i<=n;i++){
        cin>>r[i];
    }
    for(ll i{1};i<n;i++){
        ll l, k;
        cin>>l>>k;
        sons[k].emplace_back(l);
        ind[l]++;
    }
    const ll mst = [&]()->ll{
        for(ll i{1}; i<=n; i++){
            if(ind[i]==0){
                return i;
            }
        }
        throw std::runtime_error("unreachable");
    }();
    dfs(mst);
    cout<<std::max(dp[mst][0], dp[mst][1])<<'\n';
}